#!/usr/bin/env python

import os,sys,yaml,re,shutil
from datetime import datetime 
from subprocess import Popen,PIPE
if len(sys.argv) == 1:
  sys.argv.append("default.yml")
  sys.argv.append("daily")

#arguments
configfile= sys.argv[1]
TYPE      = sys.argv[2] # because of type function
#global variable
config    = yaml.load(open(configfile,'r').read())
now       = datetime.now().strftime("%Y%m%d_%H%M%S")
local_dest= config['local_destination']+"/%s/%s"%(TYPE,now)
zip_name  = "%s_%s_%s.tgz"%(config[TYPE]['prefix'],TYPE,now)
ftp_dest  = "backup"

def log(message='',fun=None,isEndPrint=False):
  print "[%s] %s %s"%(datetime.now().isoformat(),TYPE,message)
  if callable(fun):
    fun()
  if isEndPrint:
    print "[%s] %s %s end"%(datetime.now().isoformat(),TYPE,message)

def copy():
  if not os.path.exists(local_dest):
    os.makedirs(local_dest)
  targets = []
  [targets.extend(v) for k,v in config[TYPE].items()
    if re.search('^target',k)]
  for target in targets:
    log("copy from %s"%target)
    Popen(['cp','-r',target,local_dest])

  os.chdir(local_dest)
  if config[TYPE].has_key('mysql'):
    mysql = config[TYPE]['mysql']
    for db in mysql['databases']:
      log("database dump from %s"%db) 
      mysql['db']=db
      os.system("mysqldump -u%(user)s -p%(password)s %(db)s > database_%(db)s.sql"%mysql)

def process():
  os.chdir(local_dest+"/..")
  names = {'zip_name':zip_name,'now':now}
  os.system("rm -rf %(zip_name)s"%names)
  os.system("tar zcf %(zip_name)s %(now)s"%names)
  os.system("md5sum %(zip_name)s > %(zip_name)s.md5"%names)
  size = os.path.getsize(zip_name)
  log("%s size is %s bytes (%sMB)"%(zip_name,size,size/(1024**2)))
  
def upload_file(src_path,dest_path,
                ftpinfo={ 'host':'','user':'','password':''}):
  from ftplib import FTP
  ftp = FTP(ftpinfo['host'],ftpinfo['user'],ftpinfo['password'])
  ftp.storbinary("STOR %s"%dest_path,open(src_path,'rb'))
  ftp.quit()

def send():
  ftps = [i['ftp'] for i in config['destinations'] if i.has_key('ftp')]
  for ftpinfo in ftps:
    log("upload to %s"%ftpinfo['host'])
    upload_file("%s/../%s.md5"%(local_dest,zip_name),"%s/%s.md5"%(ftp_dest,zip_name),ftpinfo)
    upload_file("%s/../%s"%(local_dest,zip_name),"%s/%s"%(ftp_dest,zip_name),ftpinfo)

def clean():
  from glob import glob
  os.chdir("%s/.."%local_dest)
  shutil.rmtree(now)
  list = glob("%s_%s*"%(config[TYPE]['prefix'],TYPE))
  list.sort()
  for i in range(config[TYPE]['history']*2):
    if len(list) > 0:
      list.pop()
  for i in list:
    os.remove(i)

log('copy',copy,True)
log('process(zip,md5,log)',process, True)
log('send',send,True)
log('clean',clean)
